<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Products_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
		
	// =============================
	// = ========== GET ========== =
	// =============================
	
	function add_gallery($product_id, $img_name)
	{
		//$time = mdate('%Y-%m-%d %H:%m:%s',time());
		$data = array(
			//'id' 	=> $this->input->post('product_name'),
			'product_id'	=> $product_id,
			'image_name'	=> $img_name
			//'category_id'	=> $this->input->post('category'),
			//'app_id'		=> $this->input->post('app'),
			//'created_by'	=> $userid
		);
		
		$this->db->set($data);
		$this->db->insert('product');
	
	}
	
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
	function get_my_products($userid, $limit, $start)
	{
		$this->db->select('product.id, product.product_name, product.app_id, product.file, product.price, product.category_id, product.status, category.id as categoryid, category.category_name');
		$this->db->from('product','category');
		$this->db->join('category', 'category.id = product.category_id');
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		$this->db->where('product.created_by', $userid);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_category()
	{
		$this->db->select('id, category_name');
		$this->db->from('category');
		$query = $this->db->get();
		return $query->result();
	}
	
	function get_all_app()
	{
		$this->db->select('id, name');
		$this->db->from('app');
		$query = $this->db->get();
		return $query->result();
	}
	// --------------------------------------------------------------------
	
	/**
	 * Get all entries
	 *
	 * @access	public
	 * @return 	object	Entries
	 */
	function add_product($userid, $img_arr)
	{
		$thumbs = array_slice($img_arr, 2);
		$icon = $img_arr[0];
		$imgshow = $img_arr[1];
		if($this->input->post('alias') == '')
		{
			$alias = strtolower(auto_alias($this->input->post('productname')));
		}
		else
		{
			$alias = strtolower(auto_alias($this->input->post('alias')));
		}
		
		$time = mdate('%Y-%m-%d %H:%m:%s',time());
		$data = array(
			'product_name' 	=> $this->input->post('productname'),
			'alias'			=> $alias,
			'description'	=> $this->input->post('description'),
			'date_created'	=> $time,
			'category_id'	=> $this->input->post('category'),
			'app_id'		=> $this->input->post('app'),
			'price'			=> $this->input->post('price'),
			'created_by'	=> $userid,
			'icon'			=> $icon['file_name'],
			'imgshow'		=> $imgshow['file_name'],
			'status'		=> $this->input->post('status'),
			'featured'		=> 0,
		);
		
		$this->db->set($data);
		$this->db->insert('product');
		$product_id = $this->db->insert_id();
		
		if(count($thumbs)>0)
		{
			foreach($thumbs as $thumb)
			{
				$data = array(
					'product_id' => $product_id,
		            'image_name' => $thumb['file_name']
				);
				$this->db->insert('img_gallery', $data);
			}
		}
	}
	public function check_alias()
	{
		$alias = auto_alias(strtolower($this->input->post('alias')));
        $query =  "(
			SELECT id, alias
			FROM content WHERE alias ='$alias' )
			UNION (			
			SELECT id, alias
			FROM categories WHERE alias = '$alias')
			UNION(
			SELECT id, alias
			FROM page WHERE alias = '$alias')
			UNION(
			SELECT id, alias
			FROM category WHERE alias = '$alias')
			UNION(
			SELECT id, alias
			FROM product WHERE alias = '$alias')
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function check_productname()
	{
        $this->db->where('product_name',$this->input->post('productname'));
        $query = $this->db->get('product');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
	
	public function edit_product($productid)
	{
		if($this->input->post('alias') == '')
		{
			$alias = strtolower(auto_alias($this->input->post('productname')));
		}
		else
		{
			$alias = strtolower(auto_alias($this->input->post('alias')));
		}
		$img = $this->upload->data();
		$fields = array(
			'product_name' 	=> $this->input->post('productname'),
			'alias' 		=> $alias,
			'description'	=> $this->input->post('description'),
			'category_id'	=> $this->input->post('category'),
			'app_id'		=> $this->input->post('app'),
			'price'			=> $this->input->post('price'),
			'status'		=> $this->input->post('status'),
		);
		
		$fields2 = array(
			'product_name' 	=> $this->input->post('productname'),
			'alias' 		=> $alias,
			'description'	=> $this->input->post('description'),
			'category_id'	=> $this->input->post('category'),
			'app_id'		=> $this->input->post('app'),
			'price'			=> $this->input->post('price'),
			'status'		=> $this->input->post('status'),
			'icon'			=> $img['file_name']
		);
		
		if($img['file_name']=='')
		{
			$this->db->set($fields);
		}
		else 
		{
			$icon = $this->get_icon_product($productid);
			$file = $icon['icon'];
			@unlink('uploads/products/images/'.$file);
			$this->db->set($fields2);
		}
		$this->db->where('id', $productid);
		$this->db->update('product');
	}
	
	function get_gallery_product($producid)
	{
		$this->db->select('*');
		$this->db->where('product_id', $producid);
		$query = $this->db->get('img_gallery'); 
		return $query->result_array();
	}
	
	function delete_thumb_product($id)
	{
		$imgthumb = $this->get_thumb_item($id);
		
		$file = $imgthumb['image_name'];
		$this->db->where('id', $id);
		@unlink('uploads/products/images/'.$file);
		$this->db->delete('img_gallery');
		
	}
	
	function upload_product_thumb($productid)
	{
		$img = $this->upload->data();
		$fields = array(
			'product_id' 	=> $productid,
			'image_name'	=> $img['file_name']
		);
		$this->db->set($fields);
		$this->db->insert('img_gallery');
	}
	
	function get_thumb_item($imgid)
	{
		$this->db->select('id, image_name');
		$this->db->where('id', $imgid);
		$query = $this->db->get('img_gallery');
		return $query->row_array();
	}
	
	function get_icon_product($id)
	{
		$this->db->select('id, icon');
		$this->db->where('id', $id);
		$query = $this->db->get('product');
		return $query->row_array();
		
	}
	
	public function get_product_info($productid)
	{
		$this->db->select('product.id, ,product.price, product.product_name, product.alias, product.description, product.category_id, product.app_id, product.icon, product.status, category.id as categoryid, category.category_name');
		$this->db->from('product','category');
		$this->db->join('category', 'category.id = product.category_id');
		$this->db->where('product.id', $productid);
		$query = $this->db->get();
		return $query->row();
	}
	
	// --------------------------------------------------------------------
	/*
	 * Added by: ManhVT
	 */
	function delete_product($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('product');
		
		$this->db->where('product_id', $id);
		$this->db->delete('img_gallery');
	}
	
	public function change_status_product($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('product',$data);		
	}
	
	public function updatefile($productid)
	{

		$file = $this->upload->data();
		$fields = array(
			'file' 	=> $file['file_name'],
		);

		$this->db->set($fields);
		
		$this->db->where('id', $productid);
		$this->db->update('product');
	}
	
	public function updatefile2($productid)
	{
		$fields = array(
			'file' 	=> $this->input->post("link"),
		);

		$this->db->set($fields);
		
		$this->db->where('id', $productid);
		$this->db->update('product');
	}
	
	function count_all_my_product($userid)
	{
		$this->db->select('id');
		$this->db->from('product');
		$this->db->where('created_by', $userid);
		//$this->db->where('parent_id',0);
		return $this->db->count_all_results();
	}
	
	//end
	
	
	
	
	// ================================
	// = ========== OTHERS ========== =
	// ================================
		
	/**
	 * Check if a category exists
	 *
	 * @access	public
	 * @param	string	category name
	 * @return 	bool
	 */
	function check_category($name)
	{
		$query = $this->db->get_where($this->categories_table, array('name' => $name));
		return ($query->num_rows() > 0) ? TRUE : FALSE;
	}
}

// END listings_model.php
/* Location: ./application/models/listings_model.php */